<?php
namespace PsiFramework\Applications\AlloMediaUser\Classes\Actions;

use \PsiFramework\Classes\Util\UI\HTML;
use \PsiFramework\Classes\Util\UI\TemplateManager;
use \PsiFramework\Applications\AlloMediaUser\Classes\Actions\ShopAction;
use \PsiFramework\Classes\Util\HTTP\AuthenticationManager;
use \PsiFramework\Classes\Util\Parameters\Action\ActionParameters;
use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Plugins\Shop\Classes\Model\Order;
use \PsiFramework\Classes\Util\UI\ModuleManager;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * Classe d'action de l'index
 *
 * @package Actions.Pages
 */
class ShowBasketAction extends ShopAction
{

    public function __construct(ActionParameters $actionParameters)
    {
        parent::__construct($actionParameters);
    }

    /**
     * The main function of the method which is called every time
     * an action is created by the front controller.
     *
     */
    public function execute()
    {
        parent::execute();
        
        $orderArray = ShopAction::getOrder($this->_response->getAttribute('order'), true);
        $customerName = $orderArray['customerName'];
        $customerAddress = $orderArray['customerAddress'];
        $customerZipCode = $orderArray['customerZipCode'];
        $customerCity = $orderArray['customerCity'];
        
        if (isNullOrEmptyString($customerName)
        	|| isNullOrEmptyString($customerAddress)
        	|| isNullOrEmptyString($customerCity)
        	|| isNullOrEmptyString($customerZipCode)
        ) {
            TemplateManager::getInstance()->addLitteralScript(
            "
                var okToProceed = false;
                    
                $(document).ready(function(){
                    $('#basketForm').bind('submit', function() {
                        if(!okToProceed) {
                            var url = '".HTML::linkHref('action=AjaxBillingInformation')."' ;
                            $.ajax({
                                url: url,
                                type: 'GET',
                                cache: false,
                                timeout: 60000,
                                beforeSend: function() {
                                    $('#importMessage').html('En cours d\'envoi...')
                                },
                                complete: function(data) {
                                    $('#boxshadow-confirmation').html(data.responseText);
                                    $('#boxshadow-confirmation').modal({
                    					opacity:80,
                    					overlayCss:{backgroundColor:\"#000\"},
                    					minWidth:400,
                    					minHeight:150,
                    					maxWidth:400,
                    					maxHeight:150,
                    					overlayClose:true
                    				});
                    
                                    $('#billingForm').bind('submit', function() {
                                        $.ajax({
                                            url: $(this).attr('action'),
                                            type: $(this).attr('method'),
                                            data: $(this).serialize(),
                                            cache: false,
                                            timeout: 60000,
                                            complete: function(data) {
                                                if(data.responseText == 'ok') {
                                                    $.modal.close();                                                
                                                    okToProceed = true;
                                                } else {
                                                    alert('Error!') ;
                                                }
                                            }
                                        });
                                        return false;
                                    });
                                }
                            });
                            
                           return false;
                        }
                    });
                });
            ");
        }
        
        $this->_response->setAttribute('step', 2);
        ModuleManager::getInstance()->addModule('Steps', $this->_response);
        
        $this->findForward('success');
    }
}
